package breeze.pixel.weather.apps_util.utils;

import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.animation.BounceInterpolator;
import android.widget.Toast;

import breeze.pixel.weather.R;
import breeze.pixel.weather.apps_util.views.mtextview.MTextView;

public class MToast extends Toast {

    public String TAG = MToast.class.getSimpleName();

    private static Toast toast;

    private static MTextView text;

    public MToast(Context context) {
        super(context);
    }

    public static void show(final Context context, final CharSequence content) {
		if (content != null) {
			init(context, content);
			toast.setDuration(Toast.LENGTH_SHORT);
			text.setPivotY(0.0f);
			text.setScaleY(0.0f);
			ObjectAnimator ofFloat = ObjectAnimator.ofFloat(text, "scaleY", 0.0f, 1.0f);
			ofFloat.setStartDelay(200);
			ofFloat.setDuration(800);
			ofFloat.setInterpolator(new BounceInterpolator());
			ofFloat.start();
			toast.show();
		}
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    private static void init(Context context, CharSequence content) {
        try {
            cancelToast();
            toast = new MToast(context);
            text = new MTextView(context);
            text.setBackground(context.getResources().getDrawable(R.drawable.icon_toast, null));
            text.setGravity(Gravity.CENTER);
            text.setText(content);
            toast.setView(text);
            text.setTextColor(Color.WHITE);
            //text.setPadding(10,10,10,10);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void show(final Context context, final int content) {
		if (content != 0) {
			init(context, content);
			toast.setDuration(Toast.LENGTH_SHORT);
			text.setPivotY(0.0f);
			text.setScaleY(0.0f);
			ObjectAnimator ofFloat = ObjectAnimator.ofFloat(text, "scaleY", 0.0f, 1.0f);
			ofFloat.setStartDelay(200);
			ofFloat.setDuration(800);
			ofFloat.setInterpolator(new BounceInterpolator());
			ofFloat.start();
			toast.show();
		}
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    private static void init(Context context, int content) {
        try {
            cancelToast();
            toast = new MToast(context);
            text = new MTextView(context);
            text.setBackground(context.getResources().getDrawable(R.drawable.icon_toast, null));
            text.setGravity(Gravity.CENTER);
            text.setText(content);
            toast.setView(text);
            text.setTextColor(Color.WHITE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void cancelToast() {
        if (toast != null) {
            toast.cancel();
        }
    }

    public void cancel() {
        try {
            super.cancel();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void show() {
        try {
            super.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
